package com.atguigu.eduorder.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.eduorder.entity.Order;
import com.atguigu.eduorder.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author lhl
 * @since 2022-07-13
 */
@Slf4j
@RestController
@RequestMapping("/eduorder/order")
@CrossOrigin
public class OrderController {
    @Autowired
    OrderService orderService;
    @PostMapping("/createOrder/{orderid}")
    public R createOrder(@PathVariable String orderid, HttpServletRequest request){
        String orderId = orderService.saveOrder(orderid, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("orderId", orderId);
    }
    // 根据id获取订单接口
    @GetMapping("getOrder/{orderId}")
    public R get(@PathVariable String orderId) {
        log.info("orderId=="+orderId);
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderId);
        Order order = orderService.getOne(wrapper);
        log.info("查询数据为::"+order);
        return R.ok().data("item", order);
    }
    // 判断是否已经购买
    @GetMapping("isBuyCourse/{memberid}/{id}")
    public boolean isBuyCourse(@PathVariable String memberid,
                               @PathVariable String id) {
        int count = orderService.count(new QueryWrapper<Order>().eq("member_id", memberid).eq("course_id", id).eq("status", 1));
        return count > 0?true:false;
    }
}

